#include "Debug.h"
#include <iostream>
#include <stdarg.h>
#include <string>

std::ofstream Debug::m_outdebug;
std::ofstream Debug::m_outerror;

Debug::Debug(void)
{
	m_outdebug.open("debug.txt",std::ios::out);
	m_outerror.open("error.txt",std::ios::out);
}

Debug::~Debug(void)
{
	if (m_outdebug.is_open()) {
		m_outdebug.close();
	}
	if (m_outerror.is_open()) {
		m_outerror.close();
	}
}

void Debug::outDebug(const char* format, ...)
{
	if(!format) return;
	if(!m_outdebug.is_open()) {
		m_outdebug.open("debug.txt",std::ios::out);
	}
	va_list args;
	va_start(args, format);

	char buffer[256];
	vsnprintf(buffer,256,format, args);  //vsprintf_s

	std::string info = buffer;
#ifdef _DEBUG
	std::cout << info << std::endl;
#endif // _DEBUG
	m_outdebug << info << std::endl;
	m_outdebug.flush();
	
}


void Debug::outError(const char* format, ...)
{
	if(!format) return;
	if(!m_outerror.is_open()) {
		m_outerror.open("error.txt",std::ios::out);
	}
	va_list args;
	va_start(args, format);

	char buffer[256];
	vsnprintf(buffer,256,format, args);  //vsprintf_s

	std::string info = buffer;

	std::cout << info << std::endl;

	m_outerror << info << std::endl;
	m_outerror.flush();

}